using System;
using System.Collections.Generic;
using System.Text;

namespace VGS
{
    public class LogMessageAddedEventArgs : EventArgs
    {
        public readonly int Id;
        public readonly string Message;

        public LogMessageAddedEventArgs(int id, string message)
        {
            this.Id = id;
            this.Message = message;
        }
    }

    //public delegate void LogMessageAdded(object sender, string str);

    /// <summary>
    /// This class contains logic for logging AFTER a set has started.
    /// This is a singleton class.
    /// </summary>
    public class Log
    {
        private List<string> messages = new List<string>();
        private static Log _instance = null;

        public event EventHandler<LogMessageAddedEventArgs> MessageAdded;
        //public event LogMessageAdded Added;

        #region Constructor
        /// <summary>
        /// Private constructor to ensure singleton behaviour.
        /// Use the Instance property.
        /// </summary>
        private Log() { }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the instance of the class
        /// </summary>
        public static Log Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Log();
                }
                return _instance;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Triggers the event
        /// </summary>
        /// <param name="e">MessageAddedEventArgs</param>
        protected virtual void OnMessageAdded(LogMessageAddedEventArgs e)
        {
            if (MessageAdded != null) MessageAdded(this, e);
        }

        /// <summary>
        /// Adds a message to the log
        /// </summary>
        /// <param name="msg">The message</param>
        public void AddMessage(string msg)
        {
            //EventArgs e;
            if (msg != string.Empty)
            {
                messages.Add(msg);

                OnMessageAdded(new LogMessageAddedEventArgs(messages.Count - 1, msg));
            }
        }

        /// <summary>
        /// Gets a message from the log
        /// </summary>
        /// <param name="nr">Zero-based number of the message</param>
        /// <returns>The message at index 'nr'</returns>
        public string GetMessage(int nr)
        {
            return messages[nr];
        }
        #endregion
    }
}
